<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.rule_id ? $t('编辑') : $t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="500px" top="5vh" @close='close'>
    <div class="dialogContentBoxMaxHeight90">
      <el-form ref="form" :inline="true" :model="record" label-width="130px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('协议名称')" class="eipcipForm" prop="proto">
          <span style="color:#999;">{{ rule[record.proto] }}</span>
        </el-form-item>
        <el-form-item :label="$t('动作')" class="eipcipForm" prop="action">
          <el-select class="eipcipLength" v-model="record.action" size='small' filterable>
            <el-option v-for="(value, key) in actionList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('服务码')" class="eipcipForm" prop="cip_commond" key="cip_commond">
          <el-input v-emoji v-model="record.cip_commond" ref="commond" class="eipcipLength" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('匹配范围')" class="eipcipForm" prop="accept_range">
          <el-radio-group v-model="record.accept_range" @change="acceptRangeChange">
            <el-radio label="all" class="all">{{$t('所有')}}</el-radio>
            <el-radio label="single" class="single">{{$t('单个')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <div v-if="record.accept_range === 'single'">
          <el-form-item :label="$t('类编号')" class="eipcipForm" prop="class_id" key="class_id">
            <el-input v-emoji v-model="record.class_id" ref="id" class="eipcipLength" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('实例编号')" class="eipcipForm" prop="instance_id" key="instance_id">
            <el-input v-emoji v-model="record.instance_id" ref="instance" class="eipcipLength" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('属性编号')" class="eipcipForm" prop="attribute_id" key="attribute_id">
            <el-input v-emoji v-model="record.attribute_id" ref="attribute" class="eipcipLength" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('属性起始地址')" class="eipcipForm" prop="attribute_addr_min" key="attribute_addr_min">
            <el-input v-emoji v-model="record.attribute_addr_min" ref="min" class="eipcipLength" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('属性结束地址')" class="eipcipForm" prop="attribute_addr_max" key="attribute_addr_max">
            <el-input v-emoji v-model="record.attribute_addr_max" ref="addr" class="eipcipLength" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('数据类型')" class="eipcipForm" prop="data_type">
            <el-select class="eipcipLength" v-model="record.data_type" size='small' filterable @change="registerRangeChange">
              <el-option v-for="(value, key) in dataTypeList" :key="key" :label="value" :value="key">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('取值范围')" prop="register_value_range_select" v-if="record.data_type === 'cip_bool'" key=" register_value_range_select">
            <el-select class="eipcipLength" v-model="record.register_value_range_select" @change="registerRangeChange" size='small' filterable>
              <el-option v-for="(value, key) in registerValueRangeSelectList" :key="key" :label="$t(value)" :value="key">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('取值范围')" prop="value_range_start" v-if="record.data_type !== 'cip_bool'" key="value_range_start" style="margin-right:0">
            <el-input v-emoji v-model="record.value_range_start" ref="start" @change="registerRangeChange" style="width:117px" size='small'></el-input>
          </el-form-item>
          <el-form-item label="" v-if="record.data_type !== 'cip_bool'" style="margin-right:0">~</el-form-item>
          <el-form-item label-width="0" v-if="record.data_type !== 'cip_bool'" prop="value_range_end" key="value_range_end">
            <el-input v-emoji v-model="record.value_range_end" ref="end" @change="registerRangeChange" style="width:117px" size='small'></el-input>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>

  </el-dialog>
</template>
<script>
import ruleArray from '../../ruleArray'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'EipcipDialog',
  data () {
    var validateCipCommond = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 99) {
        callback(new Error(this.$t('输入范围0~99')))
      } else {
        callback()
      }
    }
    var validateValueRange = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < -2147483648 || Number(value) > 2147483647) {
        callback(new Error(this.$t('输入范围-2147483648~2147483647')))
      } else {
        callback()
      }
    }
    var validateId = (rule, value, callback) => {
      if (value === '') {
        callback()
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 255) {
        callback(new Error(this.$t('输入范围0~255')))
      } else {
        callback()
      }
    }
    var validateAttributeAddrMax = (rule, value, callback) => {
      let flag = this.record.attribute_addr_min
      if (value === '') {
        callback()
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 65535) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (this.record.attribute_addr_min && this.record.attribute_addr_max && (Number(this.record.attribute_addr_min) > Number(this.record.attribute_addr_max))) {
        callback(new Error(this.$t('起始地址必须小于结束地址')))
      } else {
        if (!isNaN(Number(flag)) && this.isInteger(flag) && (Number(flag) >= 0 && Number(flag) <= 65535)) {
          this.$refs['form'].clearValidate(['attribute_addr_min'])
        }
        callback()
      }
    }
    var validateAttributeAddrMin = (rule, value, callback) => {
      let flag = this.record.attribute_addr_max
      if (value === '') {
        callback()
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 65535) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (this.record.attribute_addr_min && this.record.attribute_addr_max && (Number(this.record.attribute_addr_min) > Number(this.record.attribute_addr_max))) {
        callback(new Error(this.$t('起始地址必须小于结束地址')))
      } else {
        if (!isNaN(Number(flag)) && this.isInteger(flag) && (Number(flag) >= 0 && Number(flag) <= 65535)) {
          this.$refs['form'].clearValidate(['attribute_addr_max'])
        }
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        accept_range: 'all',
        action: 'accept',
        attribute_addr_max: '',
        attribute_addr_min: '',
        attribute_id: '',
        cip_commond: '',
        class_id: '',
        data_type: 'cip_bool',
        instance_id: '',
        match_direction: '-',
        proto: 'eipcip',
        rule_id: null,
        value_range: '',
        // -----
        register_value_range_select: '0',
        value_range_end: '',
        value_range_start: ''
      },
      recordCopy: {
        accept_range: 'all',
        action: 'accept',
        attribute_addr_max: '',
        attribute_addr_min: '',
        attribute_id: '',
        cip_commond: '',
        class_id: '',
        data_type: 'cip_bool',
        instance_id: '',
        match_direction: '-',
        proto: 'eipcip',
        rule_id: null,
        value_range: '',
        // -----
        register_value_range_select: '0',
        value_range_end: '',
        value_range_start: ''
      },
      rule: ruleArray.rule,
      dataTypeList: ruleArray.dataTypeList,
      actionList: { 'accept': '允许', 'alert': '告警' },
      registerValueRangeSelectList: {
        '0': 'false',
        '1': 'true'
      },
      rules: {
        cip_commond: [{ required: false, validator: validateCipCommond, trigger: 'blur' }],
        value_range_start: [{ required: false, validator: validateValueRange, trigger: 'blur' }],
        value_range_end: [{ required: false, validator: validateValueRange, trigger: 'blur' }],
        class_id: [{ required: false, validator: validateId, trigger: 'blur' }],
        instance_id: [{ required: false, validator: validateId, trigger: 'blur' }],
        attribute_id: [{ required: false, validator: validateId, trigger: 'blur' }],
        attribute_addr_min: [{ required: false, validator: validateAttributeAddrMin, trigger: 'blur' }],
        attribute_addr_max: [{ required: false, validator: validateAttributeAddrMax, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['end', 'start', 'addr', 'attribute', 'min', 'instance', 'id', 'commond']
        inputClass(this.$refs, data)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
        this.record.value_range_start = this.record.data_type === 'cip_bool' ? '' : (this.record.value_range.split('~')[0] || '')
        this.record.value_range_end = this.record.data_type === 'cip_bool' ? '' : (this.record.value_range.split('~')[1] || '')
        this.record.register_value_range_select = this.record.data_type === 'cip_bool' ? (this.record.value_range.split('~')[0] || '0') : '0'
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          var formData = JSON.parse(JSON.stringify(this.record))
          this.$emit('addConfigRuleMap', formData)
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.$emit('childrenClose')
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    registerRangeChange () {
      if (this.record.data_type === 'cip_bool') {
        this.record.value_range = this.record.register_value_range_select + '~0'
      } else {
        this.record.value_range = this.record.value_range_start + '~' + this.record.value_range_end
      }
    },
    acceptRangeChange () {
      this.registerRangeChange()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
<style>
.eipcipLength {
  width: 250px;
}
</style>
